//
//  UIBarButtonItem+Extension.swift
//  HomeSchoolDemo
//
//  Created by 张晓林 on 16/9/27.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    /// 自定义的导航按钮
    ///
    /// - parameter imageName: 默认图片
    /// - parameter bgImgName: 背景图片
    ///
    /// - returns: 导航按钮
    convenience init(imageName: String,bgImgName: String,target:Any?,action:Selector) {
        // 初始化按钮
        let btn = UIButton(type: .custom)
        // 先获得图片
        let nomImage = UIImage(named: imageName)
        // 获得图片尺寸
        let imgSize = nomImage?.size ?? CGSize()
        // 根据图片尺寸设置frame
        btn.frame = CGRect(x: 0, y: 0, width: imgSize.width, height: imgSize.height)
        btn.setBackgroundImage(nomImage, for: .normal)
        btn.setBackgroundImage(UIImage(named: bgImgName), for: .highlighted)
        // 点击事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        // 添加按钮到初始化方法
        self.init(customView: btn)
    }
}







